@echo off
::::::::::::::::::: Information :::::::::::::::::
Rem - This publish script does the following:
::
::        - Crunshes all JavaScript & Stylesheets
::        - Zipes all filders in the source dir and removed the original
::        - Copies the Zip to a remote location
::        - Unzipes at the remote location 
::
Rem - This script uses Bat functions (see below)
Rem - For more info on Dos Functions see: http://www.dostips.com/DtTutoFunctions.php
::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::: Properties :::::::::::::::::::
set verbosity=2 & REM 3 = max
::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::: Check parameters :::::::::::::
if "%1" == "" (echo "Please specify source directory" & goto :Error)
set "source=%1"
if "%2" == "" (echo "Please specify target IP address" & goto :Error)
set "remote-ip=%2"
if "%3" == "" (echo "Please specify target SMB directory" & goto :Error)
set "remote-smb-dir=%3"
if "%4" == "" (echo "Please specify target local(ps) directory" & goto :Error)
set "remote-local-dir=%4"
set "remote-user=%5"
set "remote-password=%6"

set "pwd=%cd%"
::::::::::::::::::::::::::::::::::::::::::::::::::


:::::::::::::::::::: Main  :::::::::::::::::::::::
call:CheckIP %remote-ip% networkError
if %networkError%==0 goto Publish
if %networkError% GTR 0 goto NetworkError
echo.&pause&goto:eof

:Publish
	if %verbosity% GEQ 2 echo Preparing %source%
	call:AssetSniffer %source%
	call:Zip %source%
	call:RemoteCopy
	call:RemoteUnzip
echo.&goto:eof

:NetworkError
	echo Can not reach %2 (Is OpenVPN[192.168.110.x] or Hamachi[5.x.x.x] connected?)
echo.&goto :Error

:Error
	exit /b -1
echo.&pause&goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::

















:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:CheckIP IP
::
:: By:   Courtenay Probert, updated 2010-12-14. Version 1.0
::
:: Func: Checkes to see if an IP address is reachable
::
:: Args: %1 IP address to check (by ref)
::       %2 bool networkError (by ref)
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
	if %verbosity% GEQ 1 echo. Checking %1
	ping -n 1 -l 1 %1 > nul
	::if %errorlevel%==0 goto publish
	::if %errorlevel%==1 goto error
(endlocal
	set %2=%errorlevel%
)
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:AssetSniffer source
::
:: By:   Courtenay Probert, updated 2010-12-20. Version 1.0
::
:: Func: Find all css and JS files
::
:: Args: %1 directory containing assets (recurses subdirectories) (by ref)
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
	if %verbosity% GEQ 2 echo Sniffing Stout Scripts (in: %source%)
	for /r %source% %%i in (*.js) do call:Crunch %%i %%~xi
	if %verbosity% GEQ 2 echo .& echo Chasing Chunky Css (in: %source%)
	for /r %source% %%i in (*.css) do call:Crunch %%i %%~xi
endlocal
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Crunch resource ext
::
:: By:   Courtenay Probert, updated 2010-12-20. Version 1.0
::
:: Func: Compress a css or JS files
::
:: Args: %1 full path to assets (by ref)
::       %2 extention (by ref)
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
	if %verbosity% GEQ 3 (echo Crunching: %~1) else (if %verbosity% GEQ 2 type dot)
	SET yuicrunch=yuicompressor-2.4.2.jar
	SET crunchFile=%1
	SET ext=%2
	SET ext=%ext:.=%
	If (%ext%)==(js) SET crunchFile=%crunchFile% %
	::ECHO %crunchFile%
	SET min=%crunchFile:~-7,-4%
	::ECHO %min%
	IF NOT (%min%)==(min) (
		java -jar %pwd%\%yuicrunch% --charset utf-8 --type %ext% %crunchFile% -o %crunchFile%
	) ELSE (
		if %verbosity% GEQ 3 (echo Skiping: %crunchFile% %min% %ext%)
	)
endlocal
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Zip source
::
:: By:   Courtenay Probert, updated 2010-12-20. Version 1.0
::
:: Func: Zip source files
::
:: Args: %1 full path to source (by ref)
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
	if %verbosity% GEQ 2 echo .& echo Zipping all folders in: %source%
	for /f %%f IN ('dir /ad /b %source%') do (
		7za a %source%\%%f.7z %source%\%%f -mx9 > %source%\%%f.log 
		rmdir %source%\%%f /S /Q
	)
endlocal
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:RemoteCopy 
::
:: By:   Courtenay Probert, updated 2010-12-20. Version 1.0
::
:: Func: Copy files to remote directory
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
	if %verbosity% GEQ 2 echo Deploying zips to remote server (\\%remote-ip%%remote-smb-dir%\)
	for /r %source% %%z in (*.7z) do (
		xcopy %%z \\%remote-ip%%remote-smb-dir%\ /v /y /z 
		if not errorlevel 0 goto :Error
		del %%z /q /f
	)
endlocal
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:RemoteUnzip
::
:: By:   Courtenay Probert, updated 2010-12-20. Version 1.0
::
:: Func: Unzip zipfile on remote server
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS
	if %verbosity% GEQ 2 echo .& echo Looking for zip archives in \\%remote-ip%%remote-smb-dir%\
	for /f %%z in ('dir /B \\%remote-ip%%remote-smb-dir%\*.7z') do (
		echo .
		echo ..
		echo ...   Unzipping: %%z 
		echo .. 
		echo .
		call psexec.exe \\%remote-ip% -w %remote-local-dir% -u %remote-user% -p %remote-password% 7za -aoa x "%remote-local-dir%\%%z" 
		del \\%remote-ip%%remote-smb-dir%\%%z /q /f
	)
endlocal
goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


